package one.stand.service.impl;

import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.CollectionMapper;
import one.stand.model.CollectionModel;
import one.stand.model.ResultModel;
import one.stand.request.CollectionRequest;
import one.stand.service.OrdersService;
import one.stand.util.DateUtil;
import one.stand.util.OneConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private CollectionMapper collectionMapper;

    @Override
    public ResultModel shopAddCollection(CollectionRequest request) {
        if (request != null) {
            for (String id : request.getItemIds().split(OneConstant.arraySymbol)) {
                CollectionModel collectionModel = new CollectionModel();
                collectionModel.setUserId(request.getId().intValue());
                collectionModel.setItemType(request.getItemType());
                collectionModel.setItemId(Integer.parseInt(id));
                List<CollectionModel> list = collectionMapper.select(collectionModel);
                if (list == null || list.size() <= 0) {
                    collectionModel.setCreateTime(DateUtil.currentDate());
                    collectionMapper.insertSelective(collectionModel);
                }
            }
            return ResultModel.success("保存成功");
        }
        return ResultModel.fail();
    }
}
